package com.car.rental.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    
    // 队列名称常量，用于处理不同类型的消息
    public static final String ORDER_STATUS_QUEUE = "order.status.queue";        // 处理订单状态变更的队列
    public static final String PAYMENT_STATUS_QUEUE = "payment.status.queue";    // 处理支付状态变更的队列
    public static final String RENTAL_REMINDER_QUEUE = "rental.reminder.queue";  // 处理租期提醒的队列
    
    // 交换机名称常量，用于消息的路由分发
    public static final String ORDER_STATUS_EXCHANGE = "order.status.exchange";
    public static final String PAYMENT_STATUS_EXCHANGE = "payment.status.exchange";
    public static final String RENTAL_REMINDER_EXCHANGE = "rental.reminder.exchange";
    
    // 路由键常量，用于将消息从交换机路由到指定队列
    public static final String ORDER_STATUS_ROUTING_KEY = "order.status";
    public static final String PAYMENT_STATUS_ROUTING_KEY = "payment.status";
    public static final String RENTAL_REMINDER_ROUTING_KEY = "rental.reminder";

    // 创建订单状态队列，用于接收订单创建、支付、取消等状态变更消息
    @Bean
    public Queue orderStatusQueue() {
        return new Queue(ORDER_STATUS_QUEUE);
    }

    @Bean
    public DirectExchange orderStatusExchange() {
        return new DirectExchange(ORDER_STATUS_EXCHANGE);
    }

    @Bean
    public Binding orderStatusBinding() {
        return BindingBuilder.bind(orderStatusQueue())
                .to(orderStatusExchange())
                .with(ORDER_STATUS_ROUTING_KEY);
    }

    // 创建支付状态队列，用于接收支付成功、失败等状态变更消息
    @Bean
    public Queue paymentStatusQueue() {
        return new Queue(PAYMENT_STATUS_QUEUE);
    }

    @Bean
    public DirectExchange paymentStatusExchange() {
        return new DirectExchange(PAYMENT_STATUS_EXCHANGE);
    }

    @Bean
    public Binding paymentStatusBinding() {
        return BindingBuilder.bind(paymentStatusQueue())
                .to(paymentStatusExchange())
                .with(PAYMENT_STATUS_ROUTING_KEY);
    }

    // 创建租期提醒队列，用于处理租期到期提醒消息
    @Bean
    public Queue rentalReminderQueue() {
        return new Queue(RENTAL_REMINDER_QUEUE);
    }

    @Bean
    public DirectExchange rentalReminderExchange() {
        return new DirectExchange(RENTAL_REMINDER_EXCHANGE);
    }

    @Bean
    public Binding rentalReminderBinding() {
        return BindingBuilder.bind(rentalReminderQueue())
                .to(rentalReminderExchange())
                .with(RENTAL_REMINDER_ROUTING_KEY);
    }
} 